<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组织ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入组织ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入设备编码/位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监测指标" prop="analogName">
        <el-input
          v-model="queryParams.analogName"
          placeholder="请输入监测指标"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="analogType">
        <el-input
          v-model="queryParams.analogType"
          placeholder="请输入模拟量单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="dataStatus">
        <el-select v-model="queryParams.dataStatus" placeholder="请选择监测值状态" clearable style="width:120px">
          <el-option
            v-for="dict in monitor_data_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="监测时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeMonitorTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['alarm:data:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['alarm:data:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="组织" align="center" prop="deptName" />
      <el-table-column label="设备编码" align="center" prop="deviceCode" />
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="安装位置" align="center" prop="position" />
      <el-table-column label="监测指标" align="center" prop="analogName" />
      <el-table-column label="模拟量单位" align="center" prop="analogType" />
      <el-table-column label="监测值状态" align="center" prop="dataStatus">
        <template #default="scope">
          <dict-tag :options="monitor_data_status" :value="scope.row.dataStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="高阈值" align="center" prop="thresholdHigh" />
      <el-table-column label="低阈值" align="center" prop="thresholdLow" />
      <el-table-column label="监测值" align="center" prop="analogValue" />
      <el-table-column label="监测时间" align="center" prop="monitorTime" width="180" />
      <el-table-column label="监测说明" align="center" prop="content" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleView(scope.row)" v-hasPermi="['alarm:data:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['alarm:data:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监测数据对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="dataRef" :model="form" :rules="rules" label-width="80px" disabled>
        <el-form-item label="组织ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入组织ID" />
        </el-form-item>
        <el-form-item label="设备编码" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="安装位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入安装位置" />
        </el-form-item>
        <el-form-item label="监测指标" prop="analogName">
          <el-input v-model="form.analogName" placeholder="请输入监测指标" />
        </el-form-item>
        <el-form-item label="模拟量单位" prop="analogType">
          <el-input v-model="form.analogType" placeholder="请输入模拟量单位" />
        </el-form-item>
        <el-form-item label="监测值状态" prop="dataStatus">
          <el-radio-group v-model="form.dataStatus">
            <el-radio
              v-for="dict in monitor_data_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="高阈值" prop="thresholdHigh">
          <el-input v-model="form.thresholdHigh" placeholder="请输入高阈值" />
        </el-form-item>
        <el-form-item label="低阈值" prop="thresholdLow">
          <el-input v-model="form.thresholdLow" placeholder="请输入低阈值" />
        </el-form-item>
        <el-form-item label="监测值" prop="analogValue">
          <el-input v-model="form.analogValue" placeholder="请输入监测值" />
        </el-form-item>
        <el-form-item label="监测时间" prop="monitorTime">
          <el-input v-model="form.monitorTime" placeholder="请输入监测时间" />
        </el-form-item>
        <el-form-item label="监测内容描述" prop="content">
          <el-input v-model="form.content" placeholder="请输入监测内容描述" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Data">
import { listData, getData, delData, addData, updateData } from "@/api/alarm/data";

const { proxy } = getCurrentInstance();
const { monitor_data_status } = proxy.useDict('monitor_data_status');

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeMonitorTime = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'desc',
    deptId: null,
    searchValue: null,
    deviceCode: null,
    deviceType: null,
    position: null,
    analogName: null,
    analogType: null,
    dataStatus: null,
    monitorTime: null,
  },
  rules: {
    deptId: [
      { required: true, message: "组织ID不能为空", trigger: "blur" }
    ],
    deviceId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    deviceCode: [
      { required: true, message: "设备编码不能为空", trigger: "blur" }
    ],
    deviceType: [
      { required: true, message: "设备类型不能为空", trigger: "change" }
    ],
    position: [
      { required: true, message: "安装位置不能为空", trigger: "blur" }
    ],
    dataStatus: [
      { required: true, message: "监测值状态，0-低于阀值，1-正常范围，2-高于阀值不能为空", trigger: "change" }
    ],
    thresholdHigh: [
      { required: true, message: "高阈值不能为空", trigger: "blur" }
    ],
    thresholdLow: [
      { required: true, message: "低阈值不能为空", trigger: "blur" }
    ],
    monitorTime: [
      { required: true, message: "监测时间不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "监测内容描述不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询监测数据列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeMonitorTime && '' != daterangeMonitorTime) {
    queryParams.value.params["beginMonitorTime"] = daterangeMonitorTime.value[0];
    queryParams.value.params["endMonitorTime"] = daterangeMonitorTime.value[1];
  }
  listData(queryParams.value).then(response => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deptId: null,
    deviceId: null,
    deviceCode: null,
    deviceType: null,
    position: null,
    analogName: null,
    analogType: null,
    dataStatus: null,
    thresholdHigh: null,
    thresholdLow: null,
    analogValue: null,
    monitorTime: null,
    content: null
  };
  proxy.resetForm("dataRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeMonitorTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加监测数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getData(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改监测数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dataRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateData(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addData(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除选中的监测数据数据项？').then(function() {
    return delData(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('alarm/data/export', {
    ...queryParams.value
  }, `data_${new Date().getTime()}.xlsx`)
}


getList();
</script>
